//商品详情页
// const app = getApp(); //获取全局app.js
var WxParse = require('../../component/wxParse/wxParse.js'); //html转小程序代码

import { Detail } from 'detailModel.js';
var detail = new Detail();

//当前页面
Page({
  //页面使用的数据
  data: {
    nums: 1, //购买数量
    goodsInfo: [], //商品信息
    is_default:1, //当前选择的属性id
    selected: true, //图文详情切换
    selected2: false, //买家评价切换
    commentList:[],//评价列表
    pageNum:1,//当前页数
    hasMore:true,//评论是否完全加载完成
    baseurl: 'https://wukongweixiu.cn/',
    goods_content:'',
    price:'',
    id:'',//属性id
    userid:'',
    goodsid:''
  },

  //商品减一
  bindMinus: function () {
    var nums = this.data.nums;
    if (nums > 1) {
      nums--;
    }
    this.setData({
      nums: nums
    });
  },

  //商品加一
  bindPlus: function () {
    var nums = this.data.nums;
    nums++;
    this.setData({
      nums: nums
    });
  },
  //图文详情切换
  selected: function (e) {
    
    this.setData({
      selected2: false,
      selected: true
    });
  },
  //买家评价切换
  selected2: function (e) {
    
    this.setData({
      selected: false,
      selected2: true
    });
  },
   //规格选择
  selectSkuss:function(e){
    this.setData({
      id: e.currentTarget.dataset.id,
      price: e.currentTarget.dataset.price,
      is_default: e.currentTarget.dataset.id,
    });
  },

  //页面加载处理
  onLoad: function (options) {
    //判断访问页面时候带的商品ID
    if (options.id) {
      //设置全局商品ID
      this.setData({
        goodsId: options.id
      });
    }
    this.setData({
      userid: getApp().globalData.userid
    })
    this._getGoodsInfo(this.data.goodsId); //获取产品信息
  },


  //通过接口查询商品信息
  _getGoodsInfo: function (id) {

    var data = detail.getGoodsInfo(this.callBake,id);
  },
  callBake: function (res) {
    var that = this;
    if (res.data.code == 1) {
      this.setData({
        goodsInfo: res.data.data,
        goodsid: res.data.data.id,
        price: res.data.data.shop_price,

        goods_content: WxParse.wxParse('goods_content', 'html', res.data.data.goods_content, that, 5),
      })
    }

  },
onShow:function(){
  // this.getGoodsComment();

},
  //获取商品评价
  // getGoodsComment: function () {
  //   var data = detail.getcomment(this._callBake, this.data.goodsId);
  // },
  // _callBake: function (res) {
  //   if (res.data.code == 1) {
  //     this.setData({
  //       commentList: res.data.data.data,
  //     })
  //   }

  // },
  //加载更多评价
  commentLoading:function(){
    var that = this;
    var hasMore = that.data.hasMore;
    if (hasMore == true) {
      var page = that.data.pageNum; 
      var res = defaults.getMoregoodsAllList((res) => {
        var data = res.data.data.data;
        if (data.length < 10) {
          if (data.length > 0) {
            this.setData({
              goodsAllList: that.data.goodsAllList.concat(data),
              pageNum: page + 1,
              hasMore: false,
            });
          }
        } else {
          this.setData({
            goodsAllList: that.data.goodsAllList.concat(data),
            pageNum: page + 1
          });
        }
      }, page)
    }
  },
  //加入购物车
  goodsAddCart: function () {
    var that = this;
    var goodsid = that.data.goodsid;
    var userid =that.data.userid;
    var shuxingid = that.data.id;
    if (that.data.goodsInfo.shuxing.length > 0) {
      if (shuxingid == ''){
        wx.showToast({
          title: '请选择属性',
        })
          return false;
        }
    }
    var data = detail.goodsAddCart(this.callBakeCart, goodsid, userid, shuxingid);
  },
  callBakeCart: function (res) {
    wx.showToast({
      title: '加入成功',
    })
  },
  //加入购物车
  goCartPage: function () {
    wx.switchTab({
      url: '../cartNothing/cart'
    });
  },

  //立即购买
  buyNow: function (event) {
    var that = this;
    var goodsid = that.data.goodsid;
    var shuxingid = that.data.id;
    if (that.data.goodsInfo.shuxing.length > 0) {
      if (shuxingid == '') {
        wx.showToast({
          title: '请选择属性',
        })
        return false;
      }
    }
    wx.navigateTo({
      url: '../firmOrder/firmOrder?id=' + goodsid + '&mun=' + that.data.nums + '&shuxingid=' + shuxingid,
    });
  },
  customerService:function(){
    wx.makePhoneCall({
      phoneNumber: getApp().globalData.tile
    })
  }





});